<template>
  <div class="charge-dialog" v-if="visible">
    <div class="charge-card-box">
      <div class="charge-header">
        <span class="charge-title">会员充值</span>
        <span class="charge-close" @click="onClose">×</span>
      </div>
      <div class="charge-card">
        <!-- 充值面额选择 -->
        <div class="recharge-amount-grid">
          <div
            v-for="(item, idx) in rechargeList"
            :key="item.gearId"
            class="recharge-amount-item"
            :class="[{ active: item.gearId === selectedAmount }]"
            @click="!item.disabled && selectAmount(item.gearId)"
          >
            <div class="recharge-amount-main">
              <div class="recharge-amount">
                ￥{{ (item.rechargeAmount / 100).toFixed(2) }}
              </div>
              <div v-if="item.gearName" class="recharge-gift">
                {{ item.gearName }}
              </div>
            </div>
            <!-- <div v-if="item.desc" class="recharge-desc">{{ item.desc }}</div> -->
          </div>
        </div>
        <!-- 支付方式选择 -->
        <div class="pay-methods-bar-custom">
          <div
            v-for="(item, idx) in payBarList"
            :key="item.value"
            :class="[
              'pay-bar-custom-item',
              { active: item.value === selectedPayBar },
            ]"
            @click="!item.disabled && selectPayBar(item.value)"
          >
            <span class="pay-bar-custom-icon" v-if="item.icon">
              <img :src="item.icon" alt="" />
            </span>
            <span class="pay-bar-custom-label">{{ item.text }}</span>
          </div>
        </div>

        <!-- 完成充值按钮 -->
        <button class="recharge-confirm" @click="onConfirm">完成充值</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, defineProps, defineEmits } from 'vue';
import iconAlipay from '@/assets/pay/icon_zhifubao_nor.svg';
import iconWechat from '@/assets/pay/icon_wechat_nor.svg';
import iconCash from '@/assets/pay/icon_money_nor.svg';
import stores from '@/store';
import { recharge } from '@/api/patient';
const useBaseDataStore = stores.useBaseDataStore();
// const rechargeList = [
//   { amount: 120, gift: 100, value: 120 },
//   { amount: 260, gift: 200, value: 260 },
//   { amount: 400, gift: 300, value: 400 },
//   { amount: 1000, desc: '这里是悬停效果', value: 1000, disabled: false },
// ];
const rechargeList = ref<any[]>(useBaseDataStore.gearList.list);
//console.log(rechargeList);

const selectedAmount = ref(2);
const selectAmount = (idx: number) => {
  selectedAmount.value = idx;
};

// const payBarList = [
//   { label: '支付宝', value: 'alipay', icon: iconAlipay, disabled: false },
//   { label: '微信', value: 'wechat', icon: iconWechat, disabled: false },
//   { label: '现金', value: 'cash', icon: iconCash, disabled: false },
// ];
const payBarList = ref(
  useBaseDataStore
    .getEnumSelect('payChannelEnum')
    .map((item: any) => {
      let icon = '';

      if (item.text === '支付宝') icon = iconAlipay;
      if (item.text === '微信') icon = iconWechat;
      if (item.text === '现金') icon = iconCash;
      return { ...item, icon };
    })
    .filter((item: { value: string }) => item.value != '6001')
);
//console.log(payBarList);

const selectedPayBar = ref(0);
const selectPayBar = (idx: number) => {
  selectedPayBar.value = idx;
};

const props = defineProps({
  visible: Boolean,
  amount: { type: [String, Number], default: '0.00' },
  cashier: { type: String, default: '张三三' },
  chargeTime: { type: String, default: '2025-01-01 12:12' },
  patientId: { type: String, default: '' },
});
const emits = defineEmits(['close', 'confirm']);

const selectedMethod = ref(0);
const printRegister = ref(false);

const selectMethod = (idx: number) => {
  selectedMethod.value = idx;
};
const onClose = () => {
  emits('close');
};
const onConfirm = async () => {
  const refult = await recharge({
    patientId: props.patientId,
    gearId: selectedAmount.value,
    payChannel: selectedPayBar.value,
  });
  if (refult.code == '200') {
    emits('confirm');
    ElMessage.success('充值成功');
  } else {
    //ElMessage.error(refult.msg || '充值失败');
  }
};
</script>

<style scoped lang="scss">
.charge-dialog {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}
.charge-card {
  padding: 0px 14px 18px 14px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
}
.charge-card-box {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  background: #fff;
  border-radius: 16px;
  width: 410px;
  box-shadow: 0 4px 24px 0 rgba(0, 0, 0, 0.18);
  overflow: hidden;
}
.charge-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 16px;
  font-weight: 600;
  padding: 10px 24px;

  color: #222;
  background: linear-gradient(90deg, #e4f9f7 0%, #fffff4 100%);
}
.charge-title {
  font-size: 20px;
  font-weight: 600;
  color: #222;
}
.charge-close {
  font-size: 26px;
  color: #999;
  cursor: pointer;
  font-weight: 400;
  transition: color 0.2s;
}
.charge-close:hover {
  color: #18b3ab;
}
.charge-amount-row {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 18px;
  margin-top: 18px;
  height: 78px;
  background: rgba(231, 231, 231, 0.2);
  border-radius: 6px 6px 6px 6px;
  border: 1px solid #ffffff;
}
.charge-amount {
  font-size: 28px;
  color: #18b3ab;
  font-weight: 700;
}
/* 四格支付方式区域 */
.charge-pay-methods-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 12px;
  margin-bottom: 18px;
}
.pay-method-grid {
  display: flex;
  align-items: center;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  background: #fafbfc;
  justify-content: space-between;
  min-height: 48px;
  position: relative;
  transition: border-color 0.2s, background 0.2s;
}
.pay-method-grid.active {
  border-color: #18b3ab;
  background: #e6f7f5;
}
.pay-method-grid .pay-icon {
  margin-right: 3px;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pay-method-grid .pay-label {
  font-size: 13px;
  color: #222;
  font-weight: 500;
}
.pay-method-grid .pay-balance {
  font-size: 11px;
  color: rgba(0, 0, 0, 0.4);
  margin-left: 8px;
}
.pay-method-grid .pay-radio {
  margin-left: 10px;
  display: flex;
  align-items: center;
}
.pay-method-grid .radio-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid #e0e0e0;
  background: #fff;
  display: inline-block;
  position: relative;
  transition: border-color 0.2s;
}
.pay-method-grid.active .radio-dot {
  border-color: #18b3ab;
}
.pay-method-grid.active .radio-dot::after {
  content: '';
  display: block;
  width: 10px;
  height: 10px;
  background: #18b3ab;
  border-radius: 50%;
  position: absolute;
  left: 2px;
  top: 2px;
}
.pay-icon {
  margin-right: 3px;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pay-label {
  font-size: 16px;
  color: #222;
  font-weight: 500;
}
.pay-balance {
  font-size: 13px;
  color: #18b3ab;
  margin-left: 8px;
}
.pay-radio {
  margin-left: auto;
  display: flex;
  align-items: center;
}
.radio-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid #e0e0e0;
  background: #fff;
  display: inline-block;
  position: relative;
  transition: border-color 0.2s;
}
.radio-dot.checked {
  border-color: #18b3ab;
}
.radio-dot.checked::after {
  content: '';
  display: block;
  width: 10px;
  height: 10px;
  background: #18b3ab;
  border-radius: 50%;
  position: absolute;
  left: 3px;
  top: 3px;
}
.charge-detail-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-radius: 6px;
  padding: 10px 14px;
  font-size: 16px;
  color: #222;
  margin-bottom: 12px;

  background: #fdfdfd;
  border-radius: 6px 6px 6px 6px;
  border: 1px solid #e7e7e7;
}
.charge-detail-label {
  font-size: 13px;
  color: rgba(0, 0, 0, 0.9);
}
.charge-detail-amount {
  font-weight: 600;
  font-size: 16px;
}
.charge-info-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  color: #999;
  margin-bottom: 12px;
}
.charge-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
}
.charge-print {
  font-size: 15px;
  color: #666;
  display: flex;
  align-items: center;
  gap: 4px;
}
.charge-confirm {
  background: #18b3ab;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s;
  padding: 6px 20px;
}
.charge-confirm:hover {
  background: #139999;
}
/* 充值面额选择区域 */
.recharge-amount-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 20px;
}
.recharge-amount-item {
  box-sizing: border-box;
  background: #fff;
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  position: relative;
}
.recharge-amount-item {
  width: 119px;
  height: 78px;
  background: #ffffff;
  border-radius: 6px 6px 6px 6px;
  border: 1px solid #e7e7e7;
}
.recharge-amount-item.active {
  border-color: #18b3ab;
  background: #f7fcfa;
}
.recharge-amount-item.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.recharge-amount-main {
  text-align: center;
  padding-top: 15px;
}
.recharge-amount {
  font-weight: bold;
  font-size: 18px;
  color: rgba(0, 0, 0, 0.9);
}
.recharge-amount-item.active {
  .recharge-amount {
    color: #18b3ab;
  }
  .recharge-gift {
    color: #18b3ab;
  }
}
.recharge-gift {
  font-size: 14px;
  color: rgba(0, 0, 0, 0.6);
  font-weight: 500;
}
.recharge-desc {
  font-size: 14px;
  color: #bdbdbd;
  margin-top: 6px;
}
/* 支付方式横条 */
.pay-methods-bar {
  display: flex;
  gap: 18px;
  margin: 32px 0 24px 0;
  justify-content: center;
}
.pay-bar-item {
  display: flex;
  align-items: center;
  background: #f7f8fa;
  border-radius: 8px;
  border: 2px solid #e0e0e0;
  padding: 10px 32px;
  font-size: 18px;
  color: #222;
  font-weight: 500;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}
.pay-bar-item.active {
  border-color: #18b3ab;
  background: #e6f7f5;
  color: #18b3ab;
}
.pay-bar-item.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.pay-bar-icon {
  margin-right: 8px;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pay-bar-label {
  font-size: 18px;
  font-weight: 500;
}
.recharge-confirm {
  width: 100%;
  background: #18b3ab;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  margin-top: 18px;
  cursor: pointer;
  transition: background 0.2s;
  height: 40px;
  background: #16ada4;
  border-radius: 6px 6px 6px 6px;
}
.recharge-confirm:hover {
  background: #139999;
}

/* 自定义支付方式横条样式 */
.pay-methods-bar-custom {
  display: flex;
  gap: 18px;
  margin: 32px 0 24px 0;
  justify-content: center;
  width: 381px;
  height: 38px;
  background: #f5f5f5;
  border-radius: 3px 3px 3px 3px;
}
.pay-bar-custom-item {
  display: flex;
  align-items: center;
  font-size: 18px;
  color: #999;
  justify-content: center;
  font-weight: 500;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s, color 0.2s;
  min-width: 115px;
}
.pay-bar-custom-item.active {
  background: #ffffff;
  color: #000000;
}
.pay-bar-custom-item.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.pay-bar-custom-icon {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pay-bar-custom-label {
  font-size: 13px;
  font-weight: 500;
}
</style>
